<script setup>
import { computed, ref, watch } from "vue";
import useUserStore from "@/store/modules/user";
import useCartStore from "@/store/modules/cart";
import { getUserCart, removeItem } from "@/api/mall/client/cart/cart";
import { useRoute, useRouter } from "vue-router";
import { ElMessageBox } from "element-plus";
import { Decimal } from "decimal.js";
const keyword = ref("");
const router = useRouter();
const userStore = useUserStore();
const route = useRoute();
const searchRef = ref();
const search = () => {
  console.log("当前的路由", router.currentRoute.value.path);
  if (router.currentRoute.value.path.path === "/search") {
    console.log(searchRef.value);
    if (keyword.value !== "") {
      searchRef.value.search();
      return;
    } else {
      return;
    }
  }
  router.push({
    path: "/search",
    query: {
      keyword: keyword.value,
    },
  });
};
// 购物车
const cartStore = useCartStore();
cartStore.getCartInfo();
const totalAmount = ref(0);
const countNum = ref(0);
const calTotalAmount = () => {
  totalAmount.value = 0;
  cartStore.cartList.forEach((item) => {
    totalAmount.value = new Decimal(totalAmount.value).plus(item.totalPrice);
    countNum.value = new Decimal(countNum.value).plus(item.count);
  });
};
watch(
  () => cartStore.cartList,
  () => {
    console.log(cartStore.cartList);
    if (cartStore.cartList) {
      calTotalAmount();
    }
  },
  {
    deep: true,
  }
);
const baseUrl = import.meta.env.VITE_APP_BASE_API;

// 退出登录
const logout = () => {
  ElMessageBox.confirm("确定注销并退出系统吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      userStore.logOut().then(() => {
        location.href = "/";
      });
    })
    .catch(() => {});
};

const navs = ["普洱茶", "铁观音", "绿茶", "红茶", "花草茶", "茶具", "茶书"];
</script>

<template>
  <div class="app">
    <!-- 顶栏 -->
    <div class="public-layout">
      <div class="pub-layout-wrapper">
        <div class="pub-layout-lf">
          <div class="menu-list-lf">
            <span class="lf-menu-title" v-if="!userStore.name">欢迎来到养生茶商城</span>
            <div class="lf-menu" v-if="!userStore.name">
              <router-link to="/login" class="lf-menu-item">登录</router-link>
              <span class="lf-menu-item">注册</span>
            </div>
            <div class="lf-menu" v-if="userStore.name">
              <span class="lf-menu-item" style="color: #1aa684; font-weight: 600"
                >你好, <span>{{ userStore.name }}</span></span
              >
              <span style="color: #888888; font-weight: 600" @click="logout">退出</span>
            </div>
          </div>
        </div>
        <div class="pub-layout-rt">
          <ul class="menu-list">
            <li class="menu-item">
              <router-link to="/user?tab=Order">
                <Tickets style="width: 1em; height: 1em; margin-right: 5px" />
                <span class="menu-item-text">订单</span>
              </router-link>
            </li>
            <li class="menu-item">
              <router-link to="/user">
                <Goods style="width: 1em; height: 1em; margin-right: 5px" />
                <span class="menu-item-text">个人中心</span>
              </router-link>
            </li>
            <li class="menu-item">
              <router-link to="/points">
                <Edit style="width: 1em; height: 1em; margin-right: 5px" />
                <span class="menu-item-text">积分兑换</span>
              </router-link>
            </li>
            <li class="menu-item">
              <Service style="width: 1em; height: 1em; margin-right: 5px" />
              <span class="menu-item-text">关注我们</span>
            </li>
            <li class="menu-item" v-hasRole="['admin']">
              <router-link to="admin/index">
                <User style="width: 1em; height: 1em; margin-right: 5px" />
                <span class="menu-item-text">管理后台</span>
              </router-link>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <!-- search -->
    <section class="search-header">
      <div class="search-header-wrapper">
        <div class="s-header-lf">
          <div class="s-lf-logo">
            <img
              src="../../../assets/logo/logo-home.png"
              class="logo"
              @click="
                () => {
                  router.push({ path: '/' });
                }
              "
            />
          </div>
        </div>
        <div class="s-header-mid">
          <div class="search-container">
            <div class="search-input">
              <input type="text" v-model="keyword" placeholder="搜索商品" />
              <button type="button" @click="search">搜索</button>
            </div>
            <div class="search-footer">
              <span class="search-item"
                ><router-link to="/search">黎明普洱</router-link></span
              >
              <span class="search-item"
                ><router-link to="/search">大益</router-link></span
              >
              <span class="search-item"
                ><router-link to="/search">下关</router-link></span
              >
              <span class="search-item"
                ><router-link to="/search">七彩云南</router-link></span
              >
              <span class="search-item"
                ><router-link to="/search">紫砂</router-link></span
              >
            </div>
          </div>
        </div>
        <div class="s-header-rt">
          <div class="s-header-btn">
            <el-button
              color="#45b584"
              plain
              size="large"
              icon="ShoppingCart"
              @click="router.push({ path: '/cart' })"
            >
              我的购物车
            </el-button>
            <div class="cart-list">
              <div v-show="cartStore.countNum <= 0" class="cart-info">
                <el-empty description="暂无商品" :image-size="50" />
              </div>
              <div
                class="cart-item"
                v-for="item in cartStore.cartList"
                :key="item.skuId"
                v-show="cartStore.countNum > 0"
              >
                <el-image
                  :src="baseUrl + item.skuPic"
                  style="width: 50px; height: 50px"
                  fit="cover"
                ></el-image>
                <div class="cart-info">
                  <div class="cart-title">{{ item.title }}</div>
                  <div class="cart-attr">
                    <span v-for="attr in item.skuAttrsVals" :key="attr.attrId">{{
                      attr.attrValue
                    }}</span>
                  </div>
                </div>
                <div class="cart-num">
                  <div class="price">￥{{ item.price }}x{{ item.count }}</div>
                  <div>
                    <el-button
                      icon="Delete"
                      link
                      @click="cartStore.removeCartItem(item)"
                    ></el-button>
                  </div>
                </div>
              </div>
              <div class="cart-footer" v-show="cartStore.countNum > 0">
                <div class="total">
                  共计{{ countNum }}件商品， 合计<span class="red"
                    >￥{{ totalAmount }}元</span
                  >
                </div>
                <div>
                  <el-button
                    type="danger"
                    size="small"
                    @click="router.push({ path: '/cart' })"
                    >去结算</el-button
                  >
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
    <!-- nav -->
    <section class="nav-section">
      <div class="nav-sec-wrapper wrapper">
        <div class="nav-lf"><router-link to="/cate">全部分类</router-link></div>
        <div class="nav-rt">
          <ul class="nav-list">
            <li class="nav-list-item" v-for="item in navs" :key="item">
              <router-link to="/cate"> {{ item }} </router-link>
            </li>
          </ul>
        </div>
      </div>
    </section>

    <section class="section">
      <div class="wrapper">
        <router-view
          :keyword="keyword"
          v-slot="{ Component, route }"
          :key="route.fullPath"
        >
          <component :is="Component" ref="searchRef" />
        </router-view>
      </div>
    </section>
    <el-backtop class="backtop" :right="40" :bottom="40" />
  </div>
</template>

<style lang="scss" scoped>
.backtop {
  color: #45b584;
}
.app {
  background: #f4f4f4;
  height: 100%;
  .el-backtop {
    --el-backtop-hover-bg-color: #45b58434;
  }
}
.section {
  width: 100%;
  background: #f4f4f4;
}

.public-layout {
  color: #000;
  background-color: rgb(248, 248, 248);
  font-size: 14px;

  .pub-layout-wrapper {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    width: 1200px;
    margin: 0 auto;

    .pub-layout-lf {
      flex: 1;
      display: flex;
      justify-content: flex-start;
      align-items: center;

      .menu-list-lf {
        display: flex;

        .lf-menu {
          display: flex;
          margin-left: 10px;

          .lf-menu-item {
            margin-right: 10px;

            &:hover {
              color: #45b584;
            }
          }
        }
      }
    }

    .pub-layout-rt {
      flex: 1;
      display: flex;
      flex-direction: row;
      justify-content: flex-end;
      align-items: center;

      .menu-list {
        display: flex;

        .menu-item {
          display: flex;
          align-items: center;
          padding: 10px;

          &:hover {
            color: #45b584;
          }
        }
      }
    }
  }
}

.search-header {
  background: #fff;
  padding: 40px 0;
  .search-header-wrapper {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    align-items: flex-start;

    .s-header-lf {
      flex: 1;
      height: 100%;

      .logo {
        width: 300px;
        object-fit: cover;
      }
    }

    .s-header-mid {
      width: 50%;
      display: flex;
      height: 100%;

      .search-container {
        padding: 0 30px;
        width: 100%;
        box-sizing: border-box;

        .search-input {
          display: flex;
          align-items: center;
          border: 1px solid #1aa684;
          box-sizing: border-box;
          width: 100%;

          input {
            border: none;
            font-size: 16px;
            padding: 10px;
            // width: 360px;
            flex: 1;

            &:focus {
              outline: none;
            }
          }

          button {
            border: none;
            background: #1aa684;
            color: #fff;
            font-size: 16px;
            padding: 10px 20px;
          }
        }

        .search-footer {
          display: flex;
          margin-top: 5px;
          width: 80%;
          font-size: 14px;
          color: #1aa684;

          .search-item {
            margin-right: 15px;
          }
        }
      }
    }

    .s-header-rt {
      flex: 1;
      display: flex;
      justify-content: center;
      align-items: flex-start;
      height: 100%;

      .s-header-btn {
        width: 100%;
        text-align: center;
        position: relative;
        .cart-list {
          display: none;
        }

        &:hover {
          .cart-list {
            display: block;
            background: #fff;
            width: 340px;
            position: absolute;
            right: -124px;
            top: 35px;
            border: 1px solid #1aa684;
            height: auto;
            z-index: 33;
            padding: 10px;
            .cart-item {
              display: flex;
              padding-bottom: 5px;
              padding-top: 5px;
              border-bottom: 1px solid #dbdbdb;
              .cart-info {
                display: flex;
                flex-direction: column;
                align-items: flex-start;
                padding-left: 10px;
                width: 200px;
                overflow: hidden;
                .cart-title {
                  width: 100%;
                  overflow: hidden;
                  text-overflow: ellipsis;
                  display: -webkit-box;
                  -webkit-line-clamp: 1;
                  -webkit-box-orient: vertical;
                }
                .cart-attr {
                  color: #666;
                  font-size: 12px;
                  span {
                    padding: 0 2px;
                  }
                }
              }
              .cart-num {
                font-size: 14px;
                display: flex;
                flex-direction: column;
                flex: 1;
                align-items: flex-end;
                .price {
                  color: #dc2626;
                }
              }
            }
            .cart-footer {
              display: flex;
              flex-direction: column;
              align-items: flex-end;
              padding-top: 10px;
              .total {
                padding-bottom: 10px;
              }
            }
          }
        }
      }
    }
  }
}
.nav-section {
  width: 100%;
  border-bottom: 3px solid #1aa684;
  background: #fff;
  .nav-sec-wrapper {
    display: flex;
    align-items: center;
    background: #fff;
    .nav-lf {
      background: #1aa684;
      color: #fff;
      padding: 10px;
      width: 220px;
      text-align: center;
    }
    .nav-rt {
      flex: 1;
      .nav-list {
        display: flex;
        align-items: center;
        justify-content: space-around;
      }
    }
  }
}
.wrapper {
  margin: 0 auto;
  width: 1200px;
  // background: #fff;
}

// .banner-section {
//   padding: 0px 0;
//   height: 400px;
//   .b-sec-wrapper {
//     display: flex;
//   }

//   .b-section-cate {
//     width: 220px;
//     background: #1f2937;
//     overflow: hidden;

//     .cate-list {
//       display: flex;
//       flex-direction: column;
//       justify-content: space-between;
//       box-sizing: border-box;
//       .cate-list-item {
//         display: flex;
//         justify-content: space-between;
//         color: #fff;
//         align-items: center;
//         height: 50px;
//         padding: 0 10px 0 20px;
//         transition: all 0.3s;

//         .el-icon {
//           font-size: 20px;
//         }

//         .cate {
//           display: flex;
//           align-items: center;
//           .cate-text {
//             margin-left: 5px;
//             letter-spacing: 2px;
//           }
//         }

//         &:hover {
//           background: #fff;
//           color: #1aa684;
//         }
//       }
//     }
//   }
//   .b-section-banner {
//     width: 720px;
//     .banner {
//       width: 720px;
//       height: 400px;
//       object-fit: cover;
//     }
//   }
//   .b-section-rt {
//     flex: 1;
//     display: flex;
//     flex-direction: column;
//     padding: 0 10px;
//     overflow: hidden;
//     box-sizing: border-box;
//     .user-container {
//       height: 150px;
//       display: flex;
//       flex-direction: column;
//       align-items: center;
//       border-bottom: 1px solid #e5e7eb;
//       justify-content: center;
//       .login-box {
//         padding: 15px 0 0px 0;
//         display: flex;
//         justify-content: space-between;
//         width: 30%;
//         font-size: 14px;
//         .link {
//           transition: all 0.3s;

//           &:hover {
//             color: #45b584;
//           }
//         }
//       }
//     }
//     .notice-list {
//       width: 100%;
//       flex: 1;
//       .n-list-header {
//         display: flex;
//         justify-content: space-between;
//         font-size: 14px;
//         color: #666;
//         padding: 10px 0px 0 0px;
//       }
//       .n-list {
//         .n-item {
//           display: flex;
//           align-items: center;
//           justify-content: space-between;
//           height: 20px;
//           margin: 10px 0;
//           flex-wrap: nowrap;
//           .el-tag {
//             margin-right: 10px;
//           }
//           .notice {
//             overflow: hidden;
//             text-overflow: ellipsis;
//             white-space: nowrap;
//             transition: all 0.3s;

//             &:hover {
//               color: #45b584;
//             }
//           }
//         }
//       }
//     }
//   }
// }
// .product-section {
//   background: #f4f4f4;
//   .prd-header {
//     font-weight: 600;
//     background: #f4f4f4;
//     text-align: center;
//     padding: 20px;
//   }
//   .prd-content {
//     background: #f4f4f4;
//   }
// }

// .el-col-5 {
//   width: 20%;
//   max-width: 20%;
//   flex: 0 0 20%;
// }
</style>
